#include "EventPoller.h"
#include "EventHandler.h"
#include <algorithm>
#include "AntTweakBar.h"

EventPoller::EventPoller()
{
}

void EventPoller::Update()
{
	SDL_Event event;
    EventHandlerSet::iterator it;

    while (SDL_PollEvent(&event))
	{
        // send event to AntTweakBar
        int handled = TwEventSDL(&event);
        if( !handled ) // if event has not been handled by AntTweakBar, process it
        {
	        for( it = m_set.begin(); it != m_set.end(); ++it)
	        {
		        (*it)->HandleEvent(&event);
	        }
        }
	}
}

void EventPoller::AddHandler(EventHandler* n)
{
	m_set.insert(n);
}

void EventPoller::RemoveHandler(EventHandler* n)
{
	EventHandlerSet::iterator it;
	it = std::find(m_set.begin(),m_set.end(), n);
    if( it!= m_set.end() )
		it = m_set.erase(it);
}

void EventPoller::ClearHandlers()
{
	m_set.clear();
}